<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>数据类型 | 欢迎来到guo的博客!</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="3.数据类型1.python可变数据类型和不可变数据类型​    可变数据类型：列表，字典，集合， ​    不可变数据类型：数字，字符串，元祖 2.列表和元组的区别​    列表的数据是可变的，元组的数据是不可变的。两者的提供方法、使用场景、性能都有很大差别。在处理大数据量的时候，元组由于数据无法改变，所以就导致元组的可操作性会更强。 列表一般用于存储同构数据(具有相同意义的数据)，元组一般用于">
<meta property="og:type" content="article">
<meta property="og:title" content="数据类型">
<meta property="og:url" content="http://shangguan-lingfeng.gitee.io/blog1/2021/04/13/%E9%83%AD%E5%AE%B6%E7%91%9E%E8%83%8C%E8%AF%B5%E5%86%85%E5%AE%B9day3/index.html">
<meta property="og:site_name" content="欢迎来到guo的博客!">
<meta property="og:description" content="3.数据类型1.python可变数据类型和不可变数据类型​    可变数据类型：列表，字典，集合， ​    不可变数据类型：数字，字符串，元祖 2.列表和元组的区别​    列表的数据是可变的，元组的数据是不可变的。两者的提供方法、使用场景、性能都有很大差别。在处理大数据量的时候，元组由于数据无法改变，所以就导致元组的可操作性会更强。 列表一般用于存储同构数据(具有相同意义的数据)，元组一般用于">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-04-13T13:34:20.686Z">
<meta property="article:modified_time" content="2021-04-13T13:36:23.214Z">
<meta property="article:author" content="Jerry">
<meta property="article:tag" content="python基础知识">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/blog1/atom.xml" title="欢迎来到guo的博客!" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/blog1/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/blog1/css/style.css">

  
    
<link rel="stylesheet" href="/blog1/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog1/" id="logo">欢迎来到guo的博客!</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/blog1/">Home</a>
        
          <a class="main-nav-link" href="/blog1/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/blog1/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Suche"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Suche"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://shangguan-lingfeng.gitee.io/blog1"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-郭家瑞背诵内容day3" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/04/13/%E9%83%AD%E5%AE%B6%E7%91%9E%E8%83%8C%E8%AF%B5%E5%86%85%E5%AE%B9day3/" class="article-date">
  <time class="dt-published" datetime="2021-04-13T13:34:20.686Z" itemprop="datePublished">2021-04-13</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      数据类型
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="3-数据类型"><a href="#3-数据类型" class="headerlink" title="3.数据类型"></a>3.数据类型</h2><h4 id="1-python可变数据类型和不可变数据类型"><a href="#1-python可变数据类型和不可变数据类型" class="headerlink" title="1.python可变数据类型和不可变数据类型"></a>1.python可变数据类型和不可变数据类型</h4><p>​    可变数据类型：列表，字典，集合，</p>
<p>​    不可变数据类型：数字，字符串，元祖</p>
<h4 id="2-列表和元组的区别"><a href="#2-列表和元组的区别" class="headerlink" title="2.列表和元组的区别"></a>2.列表和元组的区别</h4><p>​    列表的数据是可变的，元组的数据是不可变的。两者的提供方法、使用场景、性能都有很大差别。在处理大数据量的时候，元组由于数据无法改变，所以就导致元组的可操作性会更强。</p>
<p>列表一般用于存储同构数据(具有相同意义的数据)，元组一般用于存储异构数据(具有不同意义的数据)</p>
<h4 id="3-说说字典的底层"><a href="#3-说说字典的底层" class="headerlink" title="3.说说字典的底层"></a>3.说说字典的底层</h4><p>​    字典是python的数据结构之一，可以将一组唯一的键找到响应的值。</p>
<p>​    CPython使用伪随机探测(计算机随机算法的随机数)的散列表(hash数组)作为字典的底层数据结构</p>
<p>​    python中所有不可变的数据类型都是可hash的，可变类型都是不可hash的。</p>
<p>​    字典的添加、获取、删除的平均事件复杂度为O(1)，但是平台最坏复杂度为O(N)</p>
<p>​    <strong>开放寻址法</strong>:因为在python3.6之前字典为无序的，可以使用开放寻址法实现hash数组。</p>
<p>​        所有的数据都存放在hash数组里，当存放产生哈希冲突时，这时会通过一个hash的一个算法计算出下一个空槽，如果下一个位置依旧哈希冲突，那么继续运算直到找到空槽进行存储该数据。</p>
<p>​    hash数组优缺点：继承了数组的优点，对CPU缓冲好，容易序列化，但内存利用率不高，且很容易造成哈希冲突。</p>
<p>​    当数据量较小时，推荐使用开放寻址法</p>
<p>​    <strong>拉链法</strong>:在python3.6版本之后字典变为有序的，底层可以使用拉链法实现。</p>
<p>​        将所有同义词关键字存放在一个单链表中，hash表中只存储单链表的头指针，遇到哈希冲突直接追加到单链表末尾，一定程度解决了哈希冲突过多的问题。</p>
<p>​        但是极特殊情况下可能在同一个链表发生大量的哈希冲突，这时查找的速度可能会变慢</p>
<p>​        红黑树：当链表过长，会升级为红黑树来加快查询速度</p>
<p>​            1.根节点是黑色节点，叶子节点是不存储数据的黑色空节点</p>
<p>​            2.任何相邻的两个节点不能同时为红色</p>
<p>​            3.任意节点到其可达到的叶子节点间包含相同数量的黑色节点</p>
<h4 id="4-字符串内置方法"><a href="#4-字符串内置方法" class="headerlink" title="4.字符串内置方法"></a>4.字符串内置方法</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># str.replace:替换字符串的指定字符为新字符</span></span><br><span class="line"><span class="string">&#x27;1*2*3*&#x27;</span>.replace(<span class="string">&#x27;*&#x27;</span>,<span class="string">&#x27;#&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 结果：1#2#3#</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># str.startswith:查找开头指定字符的字符串</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># str.split</span></span><br><span class="line"><span class="comment"># 以指定字符为分隔符，分割字符串为列表</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># str.join</span></span><br><span class="line"><span class="comment"># 以指定字符为拼接符，拼接成字符串</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># str.endswith:查找结尾指定字符的字符串</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># str.strip:删除字符串首尾空白字符</span></span><br></pre></td></tr></table></figure>


<h4 id="5-列表内置方法"><a href="#5-列表内置方法" class="headerlink" title="5.列表内置方法"></a>5.列表内置方法</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># list.append:用于在列表末尾添加新的对象</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># list.count:用于统计某个元素在列表中出现的次数</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># list.extend:拼接列表数据</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># list.index:查找列表某个值的第一次出现的下标</span></span><br><span class="line">功能、性能、优化、部署</span><br><span class="line"><span class="comment"># list.insert:在列表指定位置插入指定的值</span></span><br></pre></td></tr></table></figure>
<h4 id="6-集合内置方法"><a href="#6-集合内置方法" class="headerlink" title="6.集合内置方法"></a>6.集合内置方法</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">set</span>.difference:求两个集合的差集</span><br><span class="line">    </span><br><span class="line"><span class="built_in">set</span>.intersection:求两个集合的交集(第一个集合里有第二个集合的)</span><br><span class="line">    </span><br><span class="line"><span class="built_in">set</span>.union:求两个集合的并集(两个集合共有的)</span><br><span class="line">    </span><br><span class="line"><span class="built_in">set</span>.add:给集合添加数据</span><br><span class="line">    </span><br><span class="line"><span class="built_in">set</span>.remote:删除集合指定数据</span><br><span class="line">    </span><br><span class="line"><span class="built_in">set</span>.clear:清空集合</span><br></pre></td></tr></table></figure>
<h4 id="7-字典内置方法"><a href="#7-字典内置方法" class="headerlink" title="7.字典内置方法"></a>7.字典内置方法</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">dict</span>.keys:结果返回一个dict_keys对象，可以查看字典的所有键</span><br><span class="line">    </span><br><span class="line"><span class="built_in">dict</span>.values:结果返回一个dict_values对象，可以查看字典的所有值</span><br><span class="line">    </span><br><span class="line"><span class="built_in">dict</span>.items:结果返回一个dict_items对象，可以查看字典的键值对</span><br><span class="line">    </span><br><span class="line"><span class="built_in">dict</span>.clear清空字典</span><br><span class="line"></span><br><span class="line"><span class="built_in">dict</span>.copy浅拷贝字典</span><br><span class="line"></span><br><span class="line"><span class="built_in">dict</span>.update合并两个字典</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/04/13/%E9%83%AD%E5%AE%B6%E7%91%9E%E8%83%8C%E8%AF%B5%E5%86%85%E5%AE%B9day3/" data-id="cknublgcj0024v8uhdorm61j8" data-title="数据类型" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" rel="tag">python基础知识</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog1/2021/04/13/%E9%83%AD%E5%AE%B6%E7%91%9E%E8%83%8C%E8%AF%B5%E5%86%85%E5%AE%B9day4/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Neuer</strong>
      <div class="article-nav-title">
        
          内存管理
        
      </div>
    </a>
  
  
    <a href="/blog1/2021/04/13/%E9%83%AD%E5%AE%B6%E7%91%9E%E8%83%8C%E8%AF%B5%E5%86%85%E5%AE%B9day2/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Älter</strong>
      <div class="article-nav-title">语法基础</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/CentOS/" rel="tag">CentOS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/Flask/" rel="tag">Flask</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/django/" rel="tag">django</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/docker/" rel="tag">docker</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/gitee/" rel="tag">gitee</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/linux/" rel="tag">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/mysql/" rel="tag">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/python/" rel="tag">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" rel="tag">python基础知识</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/vue/" rel="tag">vue</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/webshell/" rel="tag">webshell</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/%E4%BA%91%E4%B8%BB%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" rel="tag">云主机实现视频直播</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/%E5%85%B3%E4%BA%8E%E7%AC%AC%E4%B8%89%E6%96%B9/" rel="tag">关于第三方</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/blog1/tags/CentOS/" style="font-size: 17.5px;">CentOS</a> <a href="/blog1/tags/Flask/" style="font-size: 10px;">Flask</a> <a href="/blog1/tags/django/" style="font-size: 12.5px;">django</a> <a href="/blog1/tags/docker/" style="font-size: 12.5px;">docker</a> <a href="/blog1/tags/gitee/" style="font-size: 10px;">gitee</a> <a href="/blog1/tags/linux/" style="font-size: 10px;">linux</a> <a href="/blog1/tags/mysql/" style="font-size: 12.5px;">mysql</a> <a href="/blog1/tags/python/" style="font-size: 15px;">python</a> <a href="/blog1/tags/python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" style="font-size: 17.5px;">python基础知识</a> <a href="/blog1/tags/redis/" style="font-size: 12.5px;">redis</a> <a href="/blog1/tags/vue/" style="font-size: 20px;">vue</a> <a href="/blog1/tags/webshell/" style="font-size: 10px;">webshell</a> <a href="/blog1/tags/%E4%BA%91%E4%B8%BB%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" style="font-size: 10px;">云主机实现视频直播</a> <a href="/blog1/tags/%E5%85%B3%E4%BA%8E%E7%AC%AC%E4%B8%89%E6%96%B9/" style="font-size: 12.5px;">关于第三方</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/02/">二月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/01/">一月 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog1/2021/05/14/docker+CentOS+mysql%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5/">docker+CentOS+mysql实现主从同步</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/13/mysql%E5%AE%9E%E7%8E%B0%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB(%E5%9C%A8%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5%E5%9F%BA%E7%A1%80%E4%B8%8A)/">mysql实现读写分离</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/12/%E5%9C%A8%E9%A1%B9%E7%9B%AE%E4%B8%AD%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3/">在项目中自动生成接口文档</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/12/mysql%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5/">mysql实现主从同步</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/09/CentOS%E9%83%A8%E7%BD%B2flask%E9%A1%B9%E7%9B%AE/">nginx部署flask项目</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 Jerry<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/blog1/" class="mobile-nav-link">Home</a>
  
    <a href="/blog1/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/blog1/js/jquery-3.4.1.min.js"></script>



  
<script src="/blog1/fancybox/jquery.fancybox.min.js"></script>




<script src="/blog1/js/script.js"></script>





  </div>
</body>
</html>